package com.jason.kotlin.gank.viewmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.jason.kotlin.gank.model.*
import com.jason.kotlin.gank.repository.CaiYunApiImpl
import com.jason.kotlin.gank.repository.RollApiImpl
import com.jason.kotlin.gank.repository.ZhiXinApiImpl
import com.jason.library.common.base.BaseViewModel
import kotlinx.coroutines.launch
import java.lang.Exception
import java.util.ArrayList

/**
 * @description: WeatherViewModel
 *
 * @author ChenBaoyang
 * @date 4/27/21 11:32
 */
class WeatherViewModel(application: Application) : BaseViewModel(application) {

    val dataHoliday = MutableLiveData<RollDayBean>()

    val dataWeather = MutableLiveData<RollWeatherBean>()

    val dataForecast = MutableLiveData<RollForecastsBean>()

    val dataLifeSuggestion = MutableLiveData<ZhiXinResultsBean>()

    val dataCaiYunWeather = MutableLiveData<CaiYunRealtimeBean>()

    val data = MutableLiveData<ArrayList<Any>>()


    private val rollApi = RollApiImpl()

    private val caiYunApi = CaiYunApiImpl()

    private val zhiXinApi = ZhiXinApiImpl()

    fun loadWeather(date: String, location: String, cityName: String) {
        viewModelScope.launch {
            showLoading()
            try {
                val datas: MutableList<Any> = ArrayList()
                val response1 = caiYunApi.getWeatherFromCaiyun(location)
                val response2 = rollApi.getCityWeatherFutureDays(cityName)
                val response3 = zhiXinApi.getLifeSuggestion(cityName)
                val response4 = rollApi.getHolidaySingle(date)
                datas.add(response1.result.realtime)
                datas.add(response2.data)
                datas.add(response3.results[0].suggestion)
                datas.add(response4.data!!)
                data.postValue(datas as ArrayList<Any>?)
                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }


        }
    }

    fun getHolidaySingle(date: String) {
        viewModelScope.launch {
            showLoading()
            try {
                val response = rollApi.getHolidaySingle(date)
                dataHoliday.postValue(response.data)

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

    fun getCityWeatherCurrentDays(cityName: String) {
        viewModelScope.launch {
            showLoading()
            try {
                val response = rollApi.getCityWeatherCurrentDays(cityName)
                dataWeather.postValue(response.data)

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

    fun getCityWeatherFutureDays(cityName: String) {
        viewModelScope.launch {
            showLoading()
            try {
                val response = rollApi.getCityWeatherFutureDays(cityName)
                dataForecast.postValue(response.data)

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

    fun getLifeSuggestion(cityName: String){
        viewModelScope.launch {
            showLoading()
            try {
                val response = zhiXinApi.getLifeSuggestion(cityName)
                dataLifeSuggestion.postValue(response.results[0])

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

    fun getWeatherFromCaiYun(location: String){
        viewModelScope.launch {
            showLoading()
            try {
                val response = caiYunApi.getWeatherFromCaiyun(location)
                dataCaiYunWeather.postValue(response.result.realtime)

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }
}